package com.bjqh.edu.UI.Activity.Index;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.Headlines;
import com.bjqh.edu.Bean.Index;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.dialog.LoadDialog;
import com.bjqh.edu.adapter.HeadlinesAdapter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGABanner;
import okhttp3.Call;
import okhttp3.Response;

public class HeadlinesActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_qx)
    TextView tvQx;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;
    @BindView(R.id.lv)
    ListView lv;
    List<Headlines.RowsBean> rowsBeen;
    HeadlinesAdapter adapter;
    List<Headlines.BannerBean> bannerBeen;
    @BindView(R.id.banner)
    BGABanner banner;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_headlines);
        ButterKnife.bind(this);

        int height = getResources().getDisplayMetrics().heightPixels;
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) banner.getLayoutParams();
        //获取当前控件的布局对象
        params.height = height / 4;//设置当前控件布局的高度
        banner.setLayoutParams(params);//将设置好的布局参数应用到控件中
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvTitle.setText("今日头条");
        banner.setAutoPlayAble(true);
        swipeLayout.setColorSchemeResources(R.color.grean);
        swipeLayout.setOnRefreshListener(this);
        getValue();

        banner.setDelegate(new BGABanner.Delegate<RelativeLayout, Index.BannerBean>() {
            @Override
            public void onBannerItemClick(BGABanner banner, RelativeLayout itemView, Index.BannerBean model, int position) {
                // Toast.makeText(banner.getContext(), "点击了" + position, Toast.LENGTH_SHORT).show();
                if (!TextUtils.isEmpty(model.getUrl())) {
                    Intent intent = new Intent();
                    intent.putExtra("url", model.getUrl());
                    intent.setClass(HeadlinesActivity.this, WebViewAcitviy.class);
                    startActivity(intent);
                }

            }
        });


    }


    @Override
    public void onRefresh() {
        getValue();
    }

    public void getValue() {
        LoadDialog.initDialog(HeadlinesActivity.this, "火速加载中...");
        OkGo.post(AppConstants.BASE_URL + "/news/datalist.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.i("今日头条", s);
                        Gson gson = new Gson();
                        swipeLayout.setRefreshing(false);
                        Headlines headlines = gson.fromJson(s, Headlines.class);
                        if (headlines.getMsg().equals("success")) {
                            LoadDialog.closeDialog();
                            rowsBeen = headlines.getRows();
                            adapter = new HeadlinesAdapter(HeadlinesActivity.this, rowsBeen);
                            lv.setAdapter(adapter);
                            AppConstants.setListViewHeightBasedOnChildren(lv);
                            bannerBeen = headlines.getBanner();
                            banner.setData(R.layout.item_banner, bannerBeen, null);
                            banner.setAdapter(new BGABanner.Adapter<RelativeLayout, Headlines.BannerBean>() {
                                @Override
                                public void fillBannerItem(BGABanner banner, RelativeLayout itemView, Headlines.BannerBean model, int position) {
                                    ImageView img = (ImageView) itemView.findViewById(R.id.img);
                                    TextView content = (TextView) itemView.findViewById(R.id.content);
                                    TextView title = (TextView) itemView.findViewById(R.id.title);
                                    LinearLayout bj = (LinearLayout) itemView.findViewById(R.id.ll);
                                    if (TextUtils.isEmpty(model.getName())) {
                                        bj.setBackgroundResource(R.color.transparent);
                                    }

                                    Glide.with(HeadlinesActivity.this).load(AppConstants.IMG_URL + model.getImg()).diskCacheStrategy(DiskCacheStrategy.ALL
                                    ).error(R.mipmap.ls).into(img);
                                    if (!TextUtils.isEmpty(model.getName()) && model.getName().contains(",")) {
                                        String[] coArray = model.getName().split(",");
                                        title.setText(coArray[0]);
                                        content.setText(coArray[1]);
                                    } else if (!TextUtils.isEmpty(model.getName())) {
                                        title.setText(model.getName());
                                    }

                                }
                            });
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Toast.makeText(HeadlinesActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                        LoadDialog.closeDialog();
                    }
                });

    }
}
